package com.itheima.demo3udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class udpServerDemo1
{
    //多发多收
    public static void main(String[] args) throws Exception {
        System.out.println("服务端启动了");
        //创建服务端
        //创建接收端对象 注册端口
        DatagramSocket ds = new DatagramSocket(10086);
        //创建一个数据包对象来接收数据
        //先创建一个字节数组
        DatagramPacket dp = null;
        while (true) {
            byte[] bys = new byte[1024*64];
            dp = new DatagramPacket(bys, bys.length);
            ds.receive(dp);
            int length = dp.getLength();//获取当前发送的数据长度
            String data = new String(bys,0,length);
            System.out.println(data);
            //获取对方ip和端口
            System.out.println(dp.getAddress().getHostAddress());
        }
        //关闭
    }
}
